{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "48149450",
   "metadata": {},
   "source": [
    "## 宿舍人脸门禁识别系统\n",
    "\n",
    "### 1.创建FaceSet（宿舍人脸库）\n",
    "查询FaceSet\n",
    "存入face_token数据\n",
    "获取FaceSet信息数据\n",
    "人脸搜索\n",
    "人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6ac16c3",
   "metadata": {},
   "source": [
    "---\n",
    "* 准备工作：四个face_token  存入FaceSet中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3bfd180d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648128320,42114275-dfa9-432f-809f-35053723ee63',\n",
       " 'time_used': 132,\n",
       " 'faces': [{'face_token': '1e5733ae10f1c3ea3d3f180eabc75eb1',\n",
       "   'face_rectangle': {'top': 333, 'left': 232, 'width': 428, 'height': 428}}],\n",
       " 'image_id': 'zKCtQfac+2OdS1Nq8dCW8Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import detect\n",
    "\n",
    "API_KEY = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_S = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "detect.face_detect(API_KEY,API_S,'1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4326d25c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648128321,871495af-c986-4dba-a13e-894c668df269',\n",
       " 'time_used': 44,\n",
       " 'faces': [{'face_token': '72f97b9388fa162716122bd1b17135a0',\n",
       "   'face_rectangle': {'top': 230, 'left': 194, 'width': 265, 'height': 265}}],\n",
       " 'image_id': '9fGGnMhJ7BAocAvAqzpxkg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'2.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "29ce757a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648128331,8851f2f8-9ccb-4f9a-839e-e9704b3c3b2e',\n",
       " 'time_used': 71,\n",
       " 'faces': [{'face_token': '0a524491a1ecdf89b5f8224c6233fb2e',\n",
       "   'face_rectangle': {'top': 253, 'left': 308, 'width': 284, 'height': 284}}],\n",
       " 'image_id': 'VappCAO/rHIxJtyewDtc4A==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'3.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9f7a4031",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648128332,44047f6d-8aa1-4957-b830-c9b3d6a0127d',\n",
       " 'time_used': 161,\n",
       " 'faces': [{'face_token': '6db2f9e7d58e25bfe26935ebfbac90d5',\n",
       "   'face_rectangle': {'top': 466, 'left': 357, 'width': 542, 'height': 542}}],\n",
       " 'image_id': 'TbteOyE4pizWvsUDXgQsNw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'4.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "de360177",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c2386801",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "API_KEY = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_S = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\" \n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'outer_id':\"dorm_faces\",\n",
    "    'display_name':\"东12-225\",\n",
    "    'tags':\"schoolgirl\",\n",
    "    'face_tokens':\"1e5733ae10f1c3ea3d3f180eabc75eb1,72f97b9388fa162716122bd1b17135a0,0a524491a1ecdf89b5f8224c6233fb2e,6db2f9e7d58e25bfe26935ebfbac90d5\",\n",
    "    'user_data':\"4人---广东\"\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ddf85224",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b1e35c29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'cdff04ea0430a8403eed1a659a1e24ce',\n",
       " 'time_used': 1077,\n",
       " 'face_count': 4,\n",
       " 'face_added': 4,\n",
       " 'request_id': '1648129115,8df24fe5-de5d-491b-8799-60a5ed7ecd9f',\n",
       " 'outer_id': 'dorm_faces',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d179dfd",
   "metadata": {},
   "source": [
    "### 2.获取人脸库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5683d68d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'cdff04ea0430a8403eed1a659a1e24ce',\n",
       " 'tags': 'schoolgirl',\n",
       " 'time_used': 101,\n",
       " 'user_data': '4人---广东',\n",
       " 'display_name': '东12-225',\n",
       " 'face_tokens': ['0a524491a1ecdf89b5f8224c6233fb2e',\n",
       "  '1e5733ae10f1c3ea3d3f180eabc75eb1',\n",
       "  '72f97b9388fa162716122bd1b17135a0',\n",
       "  '6db2f9e7d58e25bfe26935ebfbac90d5'],\n",
       " 'face_count': 4,\n",
       " 'request_id': '1648129160,f2cc5005-1db0-431d-bf9f-99ef4ce4d07a',\n",
       " 'outer_id': 'dorm_faces'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'outer_id':'dorm_faces'\n",
    "}\n",
    "\n",
    "r = requests.post(url =getdetail_url, params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee0db13e",
   "metadata": {},
   "source": [
    "### 3.人脸搜索&人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "22a77c93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648129180,75f65f9b-9946-4134-9e7d-cc0e56f7ab01',\n",
       " 'time_used': 103,\n",
       " 'faces': [{'face_token': '4870850d8e9c7cd5e0b13f5be80e575f',\n",
       "   'face_rectangle': {'top': 83, 'left': 220, 'width': 129, 'height': 129}}],\n",
       " 'image_id': 'hw22zSbw5RwZB6s+IeTDtw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 事先准备一张图片\n",
    "detect.face_detect(API_KEY,API_S,'5.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a0d6076b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648129208,cb19ff49-fc86-4fe4-adc2-b6155d4e2e73',\n",
       " 'time_used': 328,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 75.599,\n",
       "   'user_id': '',\n",
       "   'face_token': '6db2f9e7d58e25bfe26935ebfbac90d5'}]}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先搜索\n",
    "search_url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'face_token':'4870850d8e9c7cd5e0b13f5be80e575f',\n",
    "    'outer_id':\"dorm_faces\"\n",
    "}\n",
    "\n",
    "r = requests.post(url = search_url, params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a14d5da0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648129243,3610675f-9372-46b6-980a-f36f7c46a41e',\n",
       " 'time_used': 344,\n",
       " 'confidence': 75.599,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 后对比\n",
    "compare_url=\"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"face_token1\":\"4870850d8e9c7cd5e0b13f5be80e575f\",\n",
    "    \"face_token2\":\"6db2f9e7d58e25bfe26935ebfbac90d5\"\n",
    "}\n",
    "r = requests.post(url = compare_url, params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "547414f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是本宿舍成员\n"
     ]
    }
   ],
   "source": [
    "# 对比后来判断是否此人为宿舍的人--->打印\n",
    "compare_url=\"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"image_file1\":'5.jpg',\n",
    "    \"image_file2\":'4.jpg'\n",
    "}\n",
    "\n",
    "files = {\"image_file1\": open(\"5.jpg\", \"rb\"), \"image_file2\": open(\"4.jpg\", \"rb\")}\n",
    "r = requests.post(url = compare_url, params = payload, files=files)\n",
    "d = r.json()\n",
    "if d['confidence'] >= d['thresholds']['1e-5']:\n",
    "    print(\"是本宿舍成员\")\n",
    "else:\n",
    "    print(\"不是本宿舍成员\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "69edbab5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是本宿舍成员\n"
     ]
    }
   ],
   "source": [
    "# 将人脸搜索和人脸对比的代码放在在一起\n",
    "\n",
    "search_url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "compare_url=\"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload1 = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'face_token':'4870850d8e9c7cd5e0b13f5be80e575f',\n",
    "    'outer_id':\"dorm_faces\"\n",
    "}\n",
    "\n",
    "r = requests.post(url = search_url, params = payload1)\n",
    "first = r.json()\n",
    "data=first['results'][0]['face_token']\n",
    "\n",
    "payload2 = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    \"face_token1\":'4870850d8e9c7cd5e0b13f5be80e575f',\n",
    "    \"face_token2\":data2\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(url = compare_url,  params = payload2)\n",
    "second = r.json()\n",
    "if second['confidence'] >= second['thresholds']['1e-5']:\n",
    "    print(\"是本宿舍成员\")\n",
    "else:\n",
    "    print(\"不是本宿舍成员\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "de0a4745",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不是本宿舍成员\n"
     ]
    }
   ],
   "source": [
    "# 在有一个FaceSet的人脸集合前提下，进行人脸搜索和对比 判断是否为本宿舍的人。\n",
    "\n",
    "ad = detect.face_detect(API_KEY,API_S,'6.jpg')\n",
    "data1 = ad['faces'][0]['face_token']\n",
    "\n",
    "search_url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "compare_url=\"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload1 = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'face_token':data1,\n",
    "    'outer_id':\"dorm_faces\"\n",
    "}\n",
    "\n",
    "r = requests.post(url = search_url, params = payload1)\n",
    "first = r.json()\n",
    "data2 = first['results'][0]['face_token']\n",
    "\n",
    "payload2 = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    \"face_token1\":data1,\n",
    "    \"face_token2\":data2\n",
    "}\n",
    "\n",
    "r = requests.post(url = compare_url,  params = payload2)\n",
    "second = r.json()\n",
    "if second['confidence'] >= second['thresholds']['1e-5']:\n",
    "    print(\"是本宿舍成员\")\n",
    "else:\n",
    "    print(\"不是本宿舍成员\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "452c59dd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
